#define _CRT_SECURE_NO_WARNINGS

#include <string.h>
#include <iostream>
using namespace std;

namespace my_string
{
	class string
	{
	public:
		typedef char* iterator;

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		string(const char* str = "Wenn")
		{
			size_t len = strlen(str);
			char* tmp = new char[len + 1];
			_str = tmp;
			tmp = nullptr;
			strcpy(_str, str);
			_size = len;
			_capacity = len;
		}
		~string()
		{
			delete[] _str;
			_size = 0;
			_capacity = 0;
		}

		const char* c_str()
		{
			return _str;
		}

		size_t capacity()
		{
			return _capacity;
		}

		size_t size()
		{
			return _size;
		}

		void reserve(size_t n)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			tmp = nullptr;
			_capacity = n;
		}

		void push_back(char ch)
		{
			if (_size == _capacity)
			{
				reserve(_capacity * 2);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len >= _capacity)
			{
				reserve((_size + len) * 2);
			}
			char* tmp = _str;
			while (*tmp)
				tmp++;

			while (*tmp++ = *str++)
			{

			}
		}
		void append(size_t n, char ch)
		{

		}
		void append(const string& str)
		{

		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}		
		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}

		char& operator[](size_t i)
		{
			return _str[i];
		}

	private:
		char* _str;
		int _size;
		int _capacity;
	};

	void test_string1()
	{
		string s1("hello world");
		cout << s1.c_str() << endl;
		cout << s1.capacity() << endl;
		cout << s1.size() << endl;
	}

	void test_string2()
	{
		string s1("hello world");
		cout << s1.c_str() << endl;
		string::iterator it1 = s1.begin();
		while (it1 != s1.end())
		{
			(*it1)++;
			++it1;
		}
		cout << s1.c_str() << endl;

		for (auto& ch : s1)
		{
			ch--;
		}
		cout << s1.c_str() << endl;
	}

	void test_string3()
	{
		string s("abcdefg");

		for (size_t i = 0; i < s.size(); ++i)
		{
			s[i]++;
		}
		cout << s.c_str() << endl;
	}

	void test_string4()
	{
		string s1("hello");
		s1 += "!!!!!";
		cout << s1.capacity() << endl;
		cout << s1.c_str() << endl;
	}
}